// pages/login/login.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    //查看是否授权
    wx.getSetting({
      success: function (res) {
        if (res.authSetting['scope.userInfo']) {
          // console.log("用户授权了");
        } else {
          //用户没有授权
          // console.log("用户没有授权");
        }
      }
    });
  },
  bindGetUserInfo: function (res) {
    if (res.detail.userInfo) {
      //用户按了允许授权按钮
      var that = this;
      // 获取到用户的信息了，打印到控制台上看下
      app.globalData.userInfo = res.detail.userInfo
      wx.setStorage({
        data: res.detail.userInfo,
        key: 'userInfoStorage',
      })
      
      // 登录
      wx.login({
        success(res) {
          if (res.code) {
            const code = res.code
            wx.getStorage({
              key: 'bindKey',
              success: function (res) {
                console.log(res)
                let obj = {
                  code: code,
                  avatarUrl: app.globalData.userInfo.avatarUrl,
                  gender: app.globalData.userInfo.gender,
                  nickName: app.globalData.userInfo.nickName
                }
                if (res.data != undefined && res.data != '') {
                  obj.parentId = res.data
                }
                wx.request({
                  url: app.globalData.baseUrl + '/login',
                  data: obj,
                  header: {
                    'content-type': 'application/x-www-form-urlencoded' // 默认值
                  },
                  method: 'POST',
                  success(res) {
                    if (res.data.code == 0) {
                      let arr = res.data.data.split(';')
                      // console.log(arr)
                      app.globalData.xcxOpenId = arr[0]
                      wx.setStorage({
                        data: arr[0],
                        key: 'xcxOpenIdStorage',
                      })
                      if (arr[2] == 1) {
                        wx.reLaunch({
                          url: '../bindphone/bindphone'
                        })
                      } else {
                        // wx.reLaunch({
                        //   url: '../me/me'
                        // })
                        wx.reLaunch({
                          url: '../bindphone/bindphone'
                        })
                      }
    
                    }
                  }
                })
              }
            })
            
          }
        },
        fail(){
          wx.redirectTo({
            url: '../me/me',
          })
        }
      })
      //授权成功后,通过改变 isHide 的值，让实现页面显示出来，把授权页面隐藏起来
      that.setData({
        isHide: false
      });
    } else {
      //用户按了拒绝按钮
      wx.showModal({
        title: '警告',
        content: '您点击了拒绝授权，将无法进入小程序，请授权之后再进入!!!',
        showCancel: false,
        confirmText: '返回授权',
        success: function (res) {
          // 用户没有授权成功，不需要改变 isHide 的值
          if (res.confirm) {
            // console.log('用户点击了“返回授权”');
          }
        }

      });

    }

  },
  getPhoneNumber(e) {
    wx.request({
      url: app.globalData.baseUrl + '/user/bindPhone',
      data: {
        xcxOpenId: app.globalData.xcxOpenId,
        iv: e.detail.iv,
        encryptedData: e.detail.encryptedData
      },
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
        if (res.data.code == 0) {
          wx.navigateTo({
            url: '../changePhone/changePhone',
          })
        } else if (res.data.code == 999999) {
          wx.navigateTo({
            url: '../login/login',
          })
        } else {
          Toast(res.msg);
        }
      }
    })
  },
  back() {
    wx.reLaunch({
      url: '../me/me',
    })
  }
})